<?php
/**
 * Description of eventController
 *
 * @author Henson
 */
class eventController {
    
   public function indexAction()
   {
	   $event = new Event();
	   if(_checkSuperLogin() && $event->checkPermission())
	   {
			$totalNum = $event->getEventTotal();
			$p = isset($_GET['p']) ? intval($_GET['p']) : 1;
			$pageUrl = '/index.php?r=event';
			$page = new Page($p, $pageUrl, $totalNum, $event->pageRow);
			$page->run();
			$where = "where del='0'";
			$orderBy = 'order by insert_time desc, id desc';
			$limit = " limit ".($p -1 ) * $event->pageRow .",".$event->pageRow;
			$rows = $event->getEventList($where, $orderBy, $limit);
			foreach($rows as $key=>$value)
			{
				$rows[$key] = $event->dealEvent($value);
			}
			$pageInfo['rows'] = $rows;
			$pageInfo['page'] = $page;
			$view = new view(); 
			$view->showPage('management/eventList.tpl.htm', $pageInfo);
	   }
   }
   
   public function genAction()
   {
       global $_CFG;
	   $event = new Event();
	   if(_checkSuperLogin() && $event->checkPermission())
	   {
			$eid = @intval($_GET['eid']);
			if($eid > 0)
			{
				 $row = $event->getEventByEid($eid);
				 if($row)
				 {
					 $row = $event->dealEvent($row);
					 $table = new Table();
					 $row['tables'] = $table->getTableByEid($eid);
					 $pageInfo['row'] = $row;
				 }
			}
			
			if(empty($row['tables']))
			{
				$pageInfo['defaultTable'] = $_CFG["defaultTable"];
			}
			$pageInfo['address'] = $_CFG['address'];
			$pageInfo['H'] = $_CFG['H'];
			$view = new view();
			$view->showPage('management/eventAdd.tpl.htm', @$pageInfo);
	   }
   }
   
   public function showAction()
   {
	   $eid = intval($_GET['eid']);
	   $event = new Event();
	   $eventInfo = $event->getEventByEid($eid);
	   if($eventInfo)
	   {
			$eventInfo = $event->dealEvent($eventInfo);
			$eventInfo['description'] = htmlspecialchars_decode($eventInfo['description'], ENT_QUOTES);
			$table = new Table();
			$tableInfo = $table->getTableByEid($eid);
			$comment = new Comment();
			$p = isset($_GET['p']) ? intval($_GET['p']) : 1;
			$pageUrl = '/index.php?r=event/show&eid='.$eid;
			$totalNum = $comment->getCommentTotalByEid($eid);
			$comment->pageRow = 6;
			$page = new Page($p, $pageUrl, $totalNum, $comment->pageRow);
			$page->run();
			$where = "where del='0' and eid=".$eid;
			$orderBy = 'order by insert_time desc';
			$limit = " limit ".($p -1 ) * $comment->pageRow .",".$comment->pageRow;
			$commentInfo = $comment->fGetCommentList($where, $orderBy, $limit);
			foreach ($commentInfo as $key=>$item)
			{
				$commentInfo[$key] = $comment->dealComment($item);
			}
			$view = new view();
			$pageInfo['row'] = $eventInfo;
			$pageInfo['tables'] = $tableInfo;
			$pageInfo['comments'] = $commentInfo;
			$pageInfo['page'] = $page;
			$view->showPage('activity_detail.tpl.htm', @$pageInfo);
	   }
	   else
	   {
		   _showMessage('参数不对', '/index.html');
	   }
   }
   
   public function saveAction()
   {	
	   global $_CFG;
	   $event = new Event();
	   if(_checkSuperLogin() && $event->checkPermission())
	   {
			$insertEventArr = array();
			$title = @htmlspecialchars(trim($_POST['title']), ENT_QUOTES);
			empty($title) ? _errorShow('title is invalid') : $insertEventArr['title'] = $title;
			$address = intval($_POST['address']);
			array_key_exists($address, $_CFG['address']) ? $insertEventArr['address'] = $_CFG['address'][$address] : _errorShow('address is invalid');
			$insertEventArr['type'] = intval($_POST['type']);
			$insertEventArr['n_encorecard'] = intval($_POST['n_encorecard']);
			$insertEventArr['n_creditcard'] = intval($_POST['n_creditcard']);
			$insertEventArr['minors'] = intval($_POST['minors']);
			$insertEventArr['limit_time'] = intval($_POST['limit_time']);
			$insertEventArr['min'] = intval($_POST['min']);
			$start_time = $_POST['start_date'] . ' ' . $_POST['start_H'] . ':00:00';
			$end_time = $_POST['end_date'] . ' ' . $_POST['end_H'] . ':00:00';
			strtotime($start_time) ? $insertEventArr['start_time'] = strtotime($start_time) : _errorShow('period is invalid');
			strtotime($end_time) ? $insertEventArr['end_time'] = strtotime($end_time) : _errorShow('period is invalid');
			$insertEventArr['description'] = htmlentities($_POST['description']);
			$destFile = array(
				'width'		=>'',
				'height'		=>'',
				'img_name'	=>uniqid(),
				'upload_path'=>_ROOT.'/uploadfile/layout/'.date("Y-m-d").'/'
			);
			
			if(isset($_POST['eid']) && intval($_POST['eid']) > 0) //update
			{
				$eid = intval($_POST['eid']);
				if ($imgName = _uploadImage($_FILES['layout'], $destFile))
				{
					$insertEventArr['layout'] = date("Y-m-d").'/'.$imgName;
				}

				if(!$event->updateEvent($insertEventArr, $eid))
				{
					 _errorShow('event update failed');
				}
			}
			else	//insert
			{
				if(isset($_FILES['layout']) && $_FILES['layout']['error'] == 0)
				{
					$imgName = _uploadImage($_FILES['layout'], $destFile);
					if($imgName)
					{
						 $insertEventArr['layout'] = date("Y-m-d").'/'.$imgName;
						 $eid = $event->insertEvent($insertEventArr);
					}
					else
					{
						_errorShow('layout is invalid');
					}
				}
				else
				{
					 _errorShow('layout is invalid');
				}
			}

			$table = new Table();
			$table->delTableByEid($eid);
			foreach ($_POST['num'] as $key=>$value)
			{
				if(intval($value) > 0 && intval($_POST['max'][$key]) > 0)
				{
					$insertTableArr = array();
					$insertTableArr['num'] = intval($value);
					$insertTableArr['max'] = intval($_POST['max'][$key]);
					$insertTableArr['eid'] = $eid;
					if(empty($_POST['eid']))
					{
						$insertTableArr['ordered'] = intval($_POST['ordered'][$key]);
						$insertTableArr['ordered_flg'] = '1';
					}
					$table->insertTable($insertTableArr);  //eid,num  组合唯一索引
				}
			}
			$log = new Log();
			$logArr['log'] = 'create event id: '.$eid;
			$log->insertLog($logArr);
			$this->indexAction();
			die;
	   }
   }
   
   function delAction()
   {
	   $eid = @intval($_GET['eid']);
	   $event = new Event();
	   if(_checkSuperLogin() && $event->checkPermission())
	   {
		    $event->delEvent($eid);
			$log = new Log();
			$logArr['log'] = 'delete event id: '.$eid;
			$log->insertLog($logArr);
			_showMessage("删除成功", "/index.php?r=event");
	   }
   }
   
   public function showListAction()
   {
	   $event = new Event();
	   $totalNum = $event->getEventTotal();
	   $p = isset($_GET['p']) ? intval($_GET['p']) : 1;
	   $pageUrl = '/index.php?r=event/showList';
	   $event->pageRow = 10;
	   $page = new Page($p, $pageUrl, $totalNum, $event->pageRow);
	   $page->run();
	   $where = "where del='0'";
	   $orderBy = 'order by update_time desc, id desc';
	   $limit = " limit ".($p -1 ) * $event->pageRow .",".$event->pageRow;
	   $rows = $event->getEventList($where, $orderBy, $limit);
	   foreach($rows as $key=>$value)
	   {
		   $rows[$key] = $event->dealEvent($value);
	   }
	   $pageInfo['rows'] = $rows;
	   $pageInfo['page'] = $page;
	   $view = new view(); 
	   $view->showPage('activity.tpl.htm', $pageInfo);
   }
   
   public function cancelAction()
   {
	   $event = new Event();
	   if(_checkSuperLogin() && $event->checkPermission())
	   {
		   $eid = intval($_POST['eid']);
		   $note = htmlspecialchars(trim($_POST['note']), ENT_QUOTES);
		   
		   if($eid > 0 && $note != "")
		   {
			   $updateArr = array();
			   $updateArr['cancel'] = '1';
			   $updateArr['cancel_time'] = time();
			   $updateArr['cancel_note'] = $note;
			   if($event->updateEvent($updateArr, $eid))
			   {
				  $order = new Order();
				  $orderedUserInfo = $order->getOrderedUserInfoByEid($eid);
				  $mailContent = arrray();
				  $mailContent['subject'] = '活动取消';
				  $mailContent['body'] = '很抱歉，本地活动已经取消！';
				  foreach($orderedUserInfo as $key=>$value)
				  {
					  $mailContent['mailers'][] = array('name'=>$value['first_name'], 'email'=>$value['email']);
				  }
				  $log = new Log();
				  $logArr['log'] = 'cancel event id: '.$eid;
				  $log->insertLog($logArr);
				  _sendMail($mailContent);
				  _showMessage("cancel success!", '/index.php?r=event');
			   }
			   else
			   {
				   _errorShow("取消失败");
			   }
		   }
		   else
		   {
			   _errorShow("参数不正确");
		   }
	   }
   }
   
   public function reserveAction()
   {
	   $table = new Table();
	   if(_checkSuperLogin() && $table->checkPermission())
	   {
		   $tids = @htmlspecialchars(trim($_GET['tids'], ','), ENT_QUOTES);
		   $tid_temp_arr = explode(',', $tids);
		   $where = " where ordered='0' and id in ('".implode("','", $tid_temp_arr)."')";
		   $tableCount = $table->getTableNumByWhere($where);
		   if($tableCount == count($tid_temp_arr))
		   {
			   $table->updateToReserve($tid_temp_arr);
			   $log = new Log();
			   $logArr['log'] = 'reserve tables: table id is '.$tids;
			   $log->insertLog($logArr);
			   echo "success";
			   die;
		   }
		   else
		   {
			   echo "数据不符合";
			   die;
		   }
	   }
   }
   
   public function releaseAction()
   {
	   $table = new Table();
	   if(_checkSuperLogin() && $table->checkPermission())
	   {
		   $tids = @htmlspecialchars(trim($_GET['tids'], ','), ENT_QUOTES);
		   $tid_temp_arr = explode(',', $tids);
		   $where = " where ordered='1' and ordered_flg='1' and id in ('".implode("','", $tid_temp_arr)."')";
		   $tableCount = $table->getTableNumByWhere($where);
		   if($tableCount == count($tid_temp_arr))
		   {
			   $table->updateToRelease($tid_temp_arr);
			   $log = new Log();
			   $logArr['log'] = 'release tables: table id is '.$tids;
			   $log->insertLog($logArr);
			   echo "success";
			   die;
		   }
		   else
		   {
			   echo "数据不符合";
			   die;
		   }
	   }
   }
}

?>
